//
//  WYTEditingViewA.m
//  Diary
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "WYTEditingViewA.h"
#import "WYTDevicesTool.h"
#import "WYTTextFieldAlertView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

//日记编辑界面editingBoard
#define kEditingBoardViewWidth kScreenWidth * 0.95 //宽
#define kEditingBoardViewHeight kScreenHeight * 0.838 //高

//weatherButton
#define kWeatherButtonMinX kScreenWidth * 0.05
#define kWeatherButtonMinY kScreenHeight * 0.08
#define kWeatherButtonSide kScreenWidth * 0.07

//TextView
#define kTextViewWidth kEditingBoardViewWidth * 0.9
#define kTextViewHeight kEditingBoardViewHeight * 0.82
#define kTextViewMinY kEditingBoardViewHeight * 0.17

//weatherViewBoardBlurView
#define kWeatherViewBoardBlurViewSide kScreenWidth * 0.9
#define kBlurViewWeatherButtonSide kWeatherViewBoardBlurViewSide / 3

@interface WYTEditingViewA ()<UITextViewDelegate>

@property (nonatomic, strong)UIVisualEffectView *weatherViewBoardBlurView;
@property (nonatomic, strong)NSArray *weatherNameArray;

@end

@implementation WYTEditingViewA

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUps];
        [self prepareForJumpIntoDiaryInfoSelectionView];
        self.textView.delegate = self;
    }
    return self;
}

- (void)prepareForJumpIntoDiaryInfoSelectionView{
    UITapGestureRecognizer *tapToJumpInDiaryInfoView = [[UITapGestureRecognizer alloc]init];
    [tapToJumpInDiaryInfoView addTarget:self action:@selector(tapToJumpInDiaryInfoView:)];
    self.weatherIconImageView.userInteractionEnabled = YES;
    self.dateLabel.userInteractionEnabled = YES;
    [self.weatherIconImageView addGestureRecognizer:tapToJumpInDiaryInfoView];
    [self.dateLabel addGestureRecognizer:tapToJumpInDiaryInfoView];
}

- (void)setUps{
    self.weatherIconImageView = [[UIImageView alloc]init];
    self.weatherIconImageView.frame = CGRectMake(kWeatherButtonMinX, kWeatherButtonMinY, kWeatherButtonSide, kWeatherButtonSide);
    self.weatherIconImageView.image = [UIImage imageNamed:@"heavyRain_gray"];
    [self addSubview:self.weatherIconImageView];
    //再贴个button上去,要不天气图标小于30了
    UIButton *weatherButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weatherButton.frame = CGRectMake(CGRectGetMinX(self.weatherIconImageView.frame) - CGRectGetWidth(self.weatherIconImageView.frame) * ((1.5 - 1) / 2), CGRectGetMinY(self.weatherIconImageView.frame) - CGRectGetHeight(self.weatherIconImageView.frame) * ((1.5 - 1) / 2), CGRectGetWidth(self.weatherIconImageView.frame)*1.5, CGRectGetHeight(self.weatherIconImageView.frame)*1.5);
    [weatherButton addTarget:self action:@selector(weatherButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:weatherButton];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.weatherIconImageView.frame), CGRectGetMaxY(self.weatherIconImageView.frame), kEditingBoardViewWidth - (CGRectGetMinX(self.weatherIconImageView.frame) * 2), 1)];
    lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:lineView];
    
    //盖住空白的地方
    UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(lineView.frame), CGRectGetMaxY(lineView.frame), CGRectGetWidth(lineView.frame), 300)];
    [self addSubview:whiteView];
    
    self.dateLabel = [[UILabel alloc]init];
    //适配
    CGFloat labelMinYOffSetToWeatherIconScale = 0.13;
    if ([WYTDevicesTool iPhone6Plus_iPhone6sPlus]) {
        self.dateLabel.font = [UIFont systemFontOfSize:14];
    }else if ([WYTDevicesTool iPhone6_iPhone6s]){
        self.dateLabel.font = [UIFont systemFontOfSize:13];
        labelMinYOffSetToWeatherIconScale = 0.12;
    }else if ([WYTDevicesTool iPhone5_iPhone5s_iPhone5c]){
        self.dateLabel.font = [UIFont systemFontOfSize:11];
        labelMinYOffSetToWeatherIconScale = 0.1;
    }
    
    //天气选择collectionView
    self.weatherBGImageView = [[UIImageView alloc]init];
    self.weatherBGImageView.image = [UIImage imageNamed:@"baseGroundFloor"];
    self.weatherBGImageView.frame = CGRectMake(-1000, -1000, kWeatherViewBoardBlurViewSide, kWeatherViewBoardBlurViewSide);
    self.weatherBGImageView.alpha = 0;
    self.weatherBGImageView.layer.masksToBounds = YES;
    self.weatherBGImageView.layer.cornerRadius = kScreenWidth / 30;
    [self addSubview:self.weatherBGImageView];
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.weatherViewBoardBlurView = [[UIVisualEffectView alloc]initWithEffect:effect];
    self.weatherViewBoardBlurView.frame = CGRectMake(0, 0, kWeatherViewBoardBlurViewSide, kWeatherViewBoardBlurViewSide);
    self.weatherViewBoardBlurView.alpha = 1;
    [self.weatherBGImageView addSubview:self.weatherViewBoardBlurView];
    
    self.weatherNameArray = [[NSArray alloc]init];
    self.weatherNameArray = @[@"sunny_white", @"cloudy_white", @"windy_white", @"midRain_white", @"snowy_white", @"heavyRain_white", @"overcast_white", @"storm_white", @"thunder_white"];
    
    UIButton *sunnyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sunnyButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 0, kBlurViewWeatherButtonSide * 0, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [sunnyButton setImage:[UIImage imageNamed:self.weatherNameArray[0]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:sunnyButton];
    
    UIButton *cloudyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cloudyButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 1, kBlurViewWeatherButtonSide * 0, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [cloudyButton setImage:[UIImage imageNamed:self.weatherNameArray[1]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:cloudyButton];
    
    UIButton *windyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    windyButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 2, kBlurViewWeatherButtonSide * 0, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [windyButton setImage:[UIImage imageNamed:self.weatherNameArray[2]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:windyButton];
    
    UIButton *midRainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    midRainButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 0, kBlurViewWeatherButtonSide * 1, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [midRainButton setImage:[UIImage imageNamed:self.weatherNameArray[3]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:midRainButton];
    
    UIButton *snowyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    snowyButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 1, kBlurViewWeatherButtonSide * 1, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [snowyButton setImage:[UIImage imageNamed:self.weatherNameArray[4]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:snowyButton];
    
    UIButton *heavyRainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    heavyRainButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 2, kBlurViewWeatherButtonSide * 1, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [heavyRainButton setImage:[UIImage imageNamed:self.weatherNameArray[5]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:heavyRainButton];
    
    UIButton *overcastButton = [UIButton buttonWithType:UIButtonTypeCustom];
    overcastButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 0, kBlurViewWeatherButtonSide * 2, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [overcastButton setImage:[UIImage imageNamed:self.weatherNameArray[6]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:overcastButton];
    
    UIButton *stormButton = [UIButton buttonWithType:UIButtonTypeCustom];
    stormButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 1, kBlurViewWeatherButtonSide * 2, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [stormButton setImage:[UIImage imageNamed:self.weatherNameArray[7]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:stormButton];
    
    UIButton *thunderButton = [UIButton buttonWithType:UIButtonTypeCustom];
    thunderButton.frame = CGRectMake(kBlurViewWeatherButtonSide * 2, kBlurViewWeatherButtonSide * 2, kBlurViewWeatherButtonSide, kBlurViewWeatherButtonSide);
    [thunderButton setImage:[UIImage imageNamed:self.weatherNameArray[8]] forState:UIControlStateNormal];
    [self.weatherViewBoardBlurView addSubview:thunderButton];
    
    self.dateLabel.frame = CGRectMake(CGRectGetMaxX(self.weatherIconImageView.frame), CGRectGetMinY(self.weatherIconImageView.frame) + CGRectGetHeight(self.weatherIconImageView.frame) * labelMinYOffSetToWeatherIconScale, kEditingBoardViewWidth, CGRectGetWidth(self.weatherIconImageView.frame));
    self.dateLabel.text = @"2015年10月31日,星期六";
    self.dateLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    NSInteger authorLabelFontSize = 11;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        authorLabelFontSize = 10;
    } else if ([WYTDevicesTool iPhone320_DEVICES]) {
        authorLabelFontSize = 9;
    }
    self.dateLabel.font = [UIFont systemFontOfSize:authorLabelFontSize];
    [self addSubview:self.dateLabel];
    
    self.textView = [[LYLPlaceHoderTextView alloc]initWithFrame:CGRectMake((kEditingBoardViewWidth - kTextViewWidth) / 2, kTextViewMinY, kTextViewWidth, kTextViewHeight)];
    self.textView.placeholder = @"写点什么...";
    self.textView.showsVerticalScrollIndicator = NO;
    
    NSInteger textViewFontSize = 20;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        textViewFontSize = 18;
    }else if ([WYTDevicesTool iPhone5_iPhone5s_iPhone5c]){
        textViewFontSize = 16;
    }
    
    self.textView.font = [UIFont systemFontOfSize:textViewFontSize];
         UISwipeGestureRecognizer *textViewSwipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(textViewHasBeenSwipeDown:)];
    textViewSwipeDown.direction = UISwipeGestureRecognizerDirectionDown;
   [self addGestureRecognizer:textViewSwipeDown];
    
     UISwipeGestureRecognizer *textViewSwipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(textViewHasBeenSwipeUp:)];
    textViewSwipeUp.direction = UISwipeGestureRecognizerDirectionUp;
  
    [self addGestureRecognizer:textViewSwipeUp];
    [self addSubview:self.textView];
}

- (void)textViewHasBeenSwipeDown:(UISwipeGestureRecognizer *)sender{
    if ([self.textView isFirstResponder]) {
        [self.textView resignFirstResponder];
        // 还原
        self.textView.frame = CGRectMake((kEditingBoardViewWidth - kTextViewWidth) / 2, kTextViewMinY, kTextViewWidth, kTextViewHeight);
    }else{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPEDOWNTODESK" object:nil userInfo:nil];
    }
}
- (void)textViewHasBeenSwipeUp:(UISwipeGestureRecognizer *)sender{

    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPEUPTODESK" object:nil userInfo:nil];
    
}
- (void)weatherButtonDidClicked:(UIButton *)sender{
//    self.weatherBGImageView.frame = CGRectMake((kEditingBoardViewWidth - kWeatherViewBoardBlurViewSide) / 2, (kEditingBoardViewHeight - kWeatherViewBoardBlurViewSide) / 2, kWeatherViewBoardBlurViewSide, kWeatherViewBoardBlurViewSide);
//    [self bringSubviewToFront:self.weatherBGImageView];
//    [UIView animateWithDuration:0.3f animations:^{
//        self.weatherBGImageView.alpha = 0.7;
//    }];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"TAPTOJUMPINTODIARYINFOVIEW" object:nil userInfo:nil];
}

- (void)textViewDidChange:(UITextView *)textView{
    //NSLog(@"textViewDidChange");
    
    //NSLog(@"contentOffSet == %f", textView.contentOffset.y);
    if (textView.contentOffset.y > 0) {
        //NSLog(@"超出预警");
        UIView *alertView = [WYTAlertView setUpTextViewAlertView];
#warning 要修改
        [self addSubview:alertView];
    }
}

- (void)tapToJumpInDiaryInfoView:(UITapGestureRecognizer *)sender{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"TAPTOJUMPINTODIARYINFOVIEW" object:nil userInfo:nil];
    //NSLog(@"发出通知:TAPTOJUMPINTODIARYINFOVIEW");
}

@end
